package nero.yncc.com.createart.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;

import nero.yncc.com.createart.R;
import nero.yncc.com.createart.base.Constants;
import nero.yncc.com.createart.bean.BeanShopCar;
import nero.yncc.com.createart.bean.GodBean;
import nero.yncc.com.createart.custom.IconView;
import nero.yncc.com.createart.fragment.FragmentShopcar;
import nero.yncc.com.createart.interfaces.InterfaceDoSomeThing;
import nero.yncc.com.createart.utils.ImageUtils;
import nero.yncc.com.createart.utils.TextUtil;

import static android.view.View.GONE;
import static android.view.View.INVISIBLE;
import static android.view.View.VISIBLE;

/**
 * Created by nero on 17-6-23.
 */

public class AdapterShopCarRecylerView extends RecyclerView.Adapter<AdapterShopCarRecylerView.Holder> {

    Context context;
    ArrayList<BeanShopCar.goods> arrayList;

    InterfaceDoSomeThing doSomeThing;
    BeanShopCar beanShopCar;
    boolean canEdit;
    public AdapterShopCarRecylerView(Context context,BeanShopCar beanShopCar, InterfaceDoSomeThing doSomeThing,boolean canEdit) {
        this.context = context;
        this.beanShopCar=beanShopCar;
        this.arrayList = beanShopCar.getList();
        this.doSomeThing = doSomeThing;
        this.canEdit=canEdit;
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_shopcar, null);
//        view.setLayoutParams(new LinearLayout.LayoutParams(-1, -2));
        Holder holder = new Holder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final Holder holder, int position) {
        final BeanShopCar.goods godBean = arrayList.get(position);

        holder.tv_price.setText("￥" + TextUtil.getText(godBean.getShop_price(), "0"));

        holder.tv_num.setText(godBean.getNumber() + "");
        holder.tv_xnum.setText("x " + godBean.getNumber());
        if(!canEdit){
            holder.tv_num.setVisibility(INVISIBLE);
            holder.tv_cut.setVisibility(INVISIBLE);
            holder.tv_add.setVisibility(INVISIBLE);
            holder.tv_del.setVisibility(INVISIBLE);
        }
        //减少数量
        holder.tv_cut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //减少
                try {
                    int num = godBean.getNumber();
                    if (num > 1) {
                        num--;
                        godBean.setNumber(num);
                    }

                    holder.tv_num.setText(num + "");
                    holder.tv_xnum.setText("x " + num);
                    beanShopCar.setChoose(true);
                    doSomeThing.doSomething();
                } catch (Exception e) {
                }
            }
        });
        //增加数量
        holder.tv_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //减少
                try {
                    int num = godBean.getNumber();
                    if (num < Integer.parseInt(godBean.getStore_count())) {
                        num++;
                        godBean.setNumber(num);
                        holder.tv_num.setText(num + "");
                        holder.tv_xnum.setText("x " + num);
                        beanShopCar.setChoose(true);
                       doSomeThing.doSomething();
                    }

                } catch (Exception e) {

                }
            }
        });
        holder.tv_name.setText(TextUtil.getText(godBean.getGoods_name(), "暂无名称"));
        holder.tv_shuxing.setText(TextUtil.getText(godBean.getSpec_name(), "暂无简介"));
        ImageUtils.BindImage(context, holder.iv_image, Constants.IMAGE_HOST + godBean.getOriginal_img(), 1);
    }


    @Override
    public int getItemCount() {
        return arrayList.size();
    }

    class Holder extends RecyclerView.ViewHolder {
        IconView icon_choose;
        ImageView iv_image;
        TextView tv_name;
        TextView tv_shuxing;
        TextView tv_price;
        TextView tv_cut;
        TextView tv_num;
        TextView tv_add;
        TextView tv_del;
        TextView tv_xnum;
        RelativeLayout ll_root;

        public Holder(View itemView) {
            super(itemView);
            icon_choose = itemView.findViewById(R.id.icon_choose);
            iv_image = itemView.findViewById(R.id.iv_image);
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_shuxing = itemView.findViewById(R.id.tv_shuxing);
            tv_price = itemView.findViewById(R.id.tv_price);
            tv_cut = itemView.findViewById(R.id.tv_cut);
            tv_num = itemView.findViewById(R.id.tv_num);
            tv_add = itemView.findViewById(R.id.tv_add);
            tv_del = itemView.findViewById(R.id.tv_del);
            ll_root = itemView.findViewById(R.id.ll_root);
            tv_xnum = itemView.findViewById(R.id.tv_xnum);
        }
    }
}

//
//public class AdapterShopCarRecylerView extends BaseAdapter {
//
//    Context context;
//    ArrayList<BeanShopCar.goods> arrayList;
//
//    FragmentShopcar fragmentShopcar;
//
//    public AdapterShopCarRecylerView(Context context, ArrayList<BeanShopCar.goods> arrayList, FragmentShopcar fragmentShopcar) {
//        this.context = context;
//        this.arrayList = arrayList;
//        this.fragmentShopcar = fragmentShopcar;
//    }
//
//
//    @Override
//    public int getCount() {
//        return arrayList.size();
//    }
//
//    @Override
//    public Object getItem(int i) {
//        return i;
//    }
//
//    @Override
//    public long getItemId(int i) {
//        return i;
//    }
//
//    @Override
//    public View getView(int i, View view, ViewGroup viewGroup) {
//        final Holder holder;
//        if (view == null) {
//            view = LayoutInflater.from(context).inflate(R.layout.adapter_shopcar, null);
//            holder = new Holder(view);
//            view.setTag(holder);
//        } else {
//            holder = (Holder) view.getTag();
//        }
//
//        final BeanShopCar.goods godBean = arrayList.get(i);
//        holder.tv_price.setText("￥" + TextUtil.getText(godBean.getShop_price(), "0"));
//
//        if (godBean.isChoose()) {
//            holder.icon_choose.setTextColor(context.getResources().getColor(R.color.project_yellow));
//        } else {
//            holder.icon_choose.setTextColor(context.getResources().getColor(R.color.lineColorGray));
//        }
//
//        //点击选中
//        holder.icon_choose.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                godBean.setChoose(!godBean.isChoose());
//                AdapterShopCarRecylerView.this.notifyDataSetChanged();
//                fragmentShopcar.setMoney();
//            }
//        });
//        //点击选中
//        holder.ll_root.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                godBean.setChoose(!godBean.isChoose());
//                AdapterShopCarRecylerView.this.notifyDataSetChanged();
//                fragmentShopcar.setMoney();
//            }
//        });
//
//        holder.tv_num.setText(godBean.getNumber() + "");
//        holder.tv_xnum.setText("x " + godBean.getNumber());
//        //减少数量
//        holder.tv_cut.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                //减少
//                try {
//                    int num = godBean.getNumber();
//                    if (num > 1) {
//                        num--;
//                        godBean.setNumber(num);
//                    }
//
//                    holder.tv_num.setText(num + "");
//                    holder.tv_xnum.setText("x " + num);
//
//                    fragmentShopcar.setMoney();
//                } catch (Exception e) {
//                }
//            }
//        });
//        //增加数量
//        holder.tv_add.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                //减少
//                try {
//                    int num = godBean.getNumber();
//                    if (num < Integer.parseInt(godBean.getStore_count())) {
//                        num++;
//                        godBean.setNumber(num);
//                        holder.tv_num.setText(num + "");
//                        holder.tv_xnum.setText("x " + num);
//                        fragmentShopcar.setMoney();
//                    }
//
//                } catch (Exception e) {
//
//                }
//            }
//        });
//        holder.tv_name.setText(TextUtil.getText(godBean.getGoods_name(), "暂无名称"));
//        holder.tv_shuxing.setText(TextUtil.getText(godBean.getSpec_name(), "暂无简介"));
//        ImageUtils.BindImage(context, holder.iv_image, Constants.IMAGE_HOST + godBean.getOriginal_img(), 1);
//        return view;
//    }
//
//    class Holder {
//        IconView icon_choose;
//        ImageView iv_image;
//        TextView tv_name;
//        TextView tv_shuxing;
//        TextView tv_price;
//        TextView tv_cut;
//        TextView tv_num;
//        TextView tv_add;
//        TextView tv_del;
//        TextView tv_xnum;
//        RelativeLayout ll_root;
//
//        public Holder(View itemView) {
//            icon_choose = itemView.findViewById(R.id.icon_choose);
//            iv_image = itemView.findViewById(R.id.iv_image);
//            tv_name = itemView.findViewById(R.id.tv_name);
//            tv_shuxing = itemView.findViewById(R.id.tv_shuxing);
//            tv_price = itemView.findViewById(R.id.tv_price);
//            tv_cut = itemView.findViewById(R.id.tv_cut);
//            tv_num = itemView.findViewById(R.id.tv_num);
//            tv_add = itemView.findViewById(R.id.tv_add);
//            tv_del = itemView.findViewById(R.id.tv_del);
//            ll_root = itemView.findViewById(R.id.ll_root);
//            tv_xnum = itemView.findViewById(R.id.tv_xnum);
//        }
//    }
//}